import os
import sys

from fastapi import APIRouter, Depends
from loguru import logger
from requests import Session
from starlette.responses import FileResponse

from config.Database import get_db
from schemas.Result import Result

router = APIRouter(
    prefix="",
    tags=["file"],
    responses={404: {"description": "Not found"}},
)


@router.get("/download_apk")
async def download_apk():
    PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))  # 修改为向上两级目录
    apk_path = os.path.join(PROJECT_ROOT, "file", "newVersion.apk")
    if not os.path.exists(apk_path):
        logger.error(f"APK文件不存在，路径: {apk_path}")  # 添加调试信息
        raise Result.fail(msg="APK文件不存在")

    return FileResponse(
        path=apk_path,
        filename="secondClassroom.apk",
        media_type="application/vnd.android.package-archive"
    )


# 添加版本查询接口
@router.get("/version")
async def get_version():
    PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))  # 修改为向上两级目录
    file_path = os.path.join(PROJECT_ROOT, "file", "version.txt")
    try:
        if not os.path.exists(file_path):
            return Result.fail(msg="版本文件不存在")
        with open(file_path, "r") as f:
            version = f.read().strip()
        return {"success": True, "version": version}
    except Exception as e:
        logger.error(f"获取版本信息失败: {str(e)}")
        return Result.fail(msg=f"获取版本信息失败: {str(e)}")
